package com.whut.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.whut.common.Message;
import com.whut.entity.Courier;
import com.whut.entity.Express;
import com.whut.service.ExpressService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@Slf4j
@RequestMapping("/express")
public class ExpressController {
    @Autowired
    private ExpressService expressService;

    /**
     * 显示快递列表
     * @param offset
     * @param pageNumber
     * @return
     */
    @GetMapping("/list.do")
    @ResponseBody
    public List<Express> page(int offset, int pageNumber){
        log.info("express分页：pageNum={},pageSize={}",offset,pageNumber);
//        Page<Express> page=new Page<>(pageNum,pageSize);
        final LambdaQueryWrapper<Express> queryWrapper=new LambdaQueryWrapper<>();
        //构造分页条件
//        select * from express limit offset,pageNumber;
        queryWrapper.last("limit "+offset+","+pageNumber);
//        queryWrapper.orderByDesc(Express::getUpdateTime);
        //获取分页信息
        List<Express> expresses=expressService.list(queryWrapper);

        log.info("pagelist={}",expresses.toString());
        return expresses;
    }

    /**
     * 添加快递
     * @param express
     * @return
     */
    @PostMapping("/insert.do")
//    @ResponseBody
    public String add(@RequestBody Express express){
//        String number = request.getParameter("number");
//        String company = request.getParameter("companyName");
//        String username = request.getParameter("name");
//        String userPhone = request.getParameter("phone");
//        Express express=new Express();
//        express.setNumber(number);
//        express.setCompanyName(company);
//        express.setName(username);
//        express.setPhone(userPhone);
        //设置status
        express.setStatus(1);
        //设置初始取件码
        express.setCode(String.valueOf(IdWorker.getId()));
        log.info("添加express={}",express);
//        express.setOutTime(LocalDateTime.now());
        boolean flag=expressService.save(express);
        if(flag){
            return "success";
        }else {
            return "快递添加成功";
        }
    }

    /**
     * 根据运单号查找快递
     * @param request
     * @return
     */
    @GetMapping("/find.do")
    public Message<Express> hasUser(HttpServletRequest request){

        String number=request.getParameter("number");
        log.info("express进入查询函数，phone={}",number);
        LambdaQueryWrapper<Express> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(number!=null,Express::getNumber,number);
        Express express=expressService.getOne(queryWrapper);
        //未查到
        if(express == null){
            return Message.error("未查到");
        }
        return Message.success(express);
    }


    /**
     * 快递更新
     * @param express
     * @return
     */
    @PostMapping("/update.do")
    public String update(@RequestBody Express express){
//        express.setLoginTime(LocalDateTime.now());
        log.info("进入更新express函数，接收参数：{}",express);
        expressService.updateById(express);
        return "success";
    }

    /**
     * 快递删除
     * @param request
     * @return
     */
    @GetMapping("/delete.do")
    public String delete(HttpServletRequest request){
        String number=request.getParameter("number");
        log.info("进入express的删除函数，number={}",number);
        LambdaQueryWrapper<Express> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(number!=null,Express::getNumber,number);
        boolean flag= expressService.remove(queryWrapper);
        return flag==true?"success":"false";
    }

    /**
     * 控制台函数，查询全部快递信息
     * @param request
     * @param response
     * @return
     */
    @ResponseBody
    @PostMapping("/console.do")
    public Message console(HttpServletRequest request, HttpServletResponse response){
        //查询全部快递信息（总数、新增）
        LambdaQueryWrapper<Express> queryWrapper = new LambdaQueryWrapper<>();
        Integer data1_size = expressService.count();
        queryWrapper.ge(Express::getCreateTime, LocalDate.now());
        Integer data1_day = expressService.count(queryWrapper);

        //查询待取件的快递信息（总数、新增）
        LambdaQueryWrapper<Express> expressLambdaQueryWrapper = new LambdaQueryWrapper<>();
        expressLambdaQueryWrapper.eq(Express::getStatus, 3);
        Integer data2_size = expressService.count(expressLambdaQueryWrapper);
        expressLambdaQueryWrapper.ge(Express::getOutTime, LocalDate.now());
        Integer data2_day = expressService.count(expressLambdaQueryWrapper);

        //组装数据
        ArrayList<Map<String,Integer>> data = new ArrayList<>();
        Map data1 = new HashMap();
        data1.put("data1_size",data1_size);
        data1.put("data1_day",data1_day);
        Map data2 = new HashMap();
        data2.put("data2_size",data2_size);
        data2.put("data2_day",data2_day);
        data.add(data1);
        data.add(data2);

        Message msg = new Message();
        Integer status = data.size() == 0 ? -1 : 0;
        msg.setStatus(status);
        msg.setData(data);

        return msg;
    }

}
